<?php

// 公共助手函数

use Symfony\Component\VarExporter\VarExporter;
use think\Db;
/**
 * 直推奖
 * @param decimal $money 消费金额
 * @param int $userid 消费者ID
 */
if (!function_exists('direct_push_reward')){
    function direct_push_reward($money,$userid,$orderid){
        $money = abs($money);
        if($money >0.01){
            //查询上级
            $user_info = \app\common\model\User::where('id',$userid)->field('parent_id')->find();
            if($user_info){
                $parent_info = \app\common\model\User::where('id',$user_info['parent_id'])->find();
                if($parent_info){
                    //查询直推奖比例
                    $direct_reward = \app\common\model\Reward::where('id',1)->value('proportion');
                    if(isset($direct_reward) && $direct_reward >0){
                        $reward = $money*$direct_reward/100;
                        if($reward >0.01){
                            //添加奖励记录，上级增加余额
                            $data = [
                                'userid'=>$parent_info['id'],
                                'money'=>$reward,
                                'type'=>1,
                                'order_id'=>$orderid,
                            ];
                            Db::startTrans();
                            try {
                                $res = \app\common\model\UserMoneyLog::create($data);
                                if($res){
                                    $res1 = \app\common\model\User::where('id',$parent_info['id'])->setInc('money',$reward);

                                }
                                Db::commit();
                            } catch (Exception $e) {
                                return $e->getMessage();
                                Db::rollback();
                                return $e->getMessage();
                            }
                            if($res && $res1){
                                return true;
                            }
                        }
                    }
                }
            }
        }
       return false;
    }
}
/**
 * 间推奖
 * @param decimal $money 消费金额
 * @param int $userid 消费者ID
 */
if (!function_exists('indirect_reward')){
    function indirect_reward($money,$userid,$orderid){
        $money = abs($money);
        if($money >0.01){
            //查询上级
            $user_info = \app\common\model\User::where('id',$userid)->field('parent_id')->find();
            if($user_info){
                $parent_info = \app\common\model\User::where('id',$user_info['parent_id'])->find();
                if($parent_info){
                    //查询上级的上级
                    if($parent_info && $parent_info['parent_id'] >0){
                        $grand_parent_info = \app\common\model\User::where('id',$parent_info['parent_id'])->find();
                        if($grand_parent_info){
                            //查询间推奖比例
                            $direct_reward = \app\common\model\Reward::where('id',2)->value('proportion');
                            if(isset($direct_reward) && $direct_reward >0){
                                $reward = $money*$direct_reward/100;
                                if($reward >0.01){
                                    //添加奖励记录，上级的上级增加余额
                                    $data = [
                                        'userid'=>$grand_parent_info['id'],
                                        'money'=>$reward,
                                        'type'=>2,
                                        'order_id'=>$orderid
                                    ];
                                    Db::startTrans();
                                    try {
                                        $res = \app\common\model\UserMoneyLog::create($data);
                                        if($res){
                                            $res1 = \app\common\model\User::where('id',$grand_parent_info['id'])->setInc('money',$reward);
                                        }
                                        Db::commit();
                                    } catch (Exception $e) {
                                        return $e->getMessage();
                                        Db::rollback();
                                        return $e->getMessage();
                                    }
                                    if($res && $res1){
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}
/**
 * 级差奖
 * @param decimal $money 消费金额
 * @param int $userid 消费者ID
 */
if (!function_exists('difference_reward')){
    function difference_reward($money,$userid,$orderid){
        $money = abs($money);
        if($money >0.01){
            $user_info = \app\common\model\User::where('id',$userid)->field('id,group_id,path')->find();
            if($user_info['path'] != ''){
                $path = $user_info['path'].','.$userid;
            }else{
                $path = $userid;
            }
            $path_arr = explode(',',$path);
            //按照键名降序排序
            krsort($path_arr);
            //查看当前消费者是什么等级会员，如果是三级会员，只有三类级差奖，如果是二级会员，有二级级差和三级级差奖，如果是一级会员或者普通会员，有一级级差奖、二级级差奖、三级级差奖。
            jicha_level($money,$userid,$path_arr,$orderid);
            return true;
        }
        return false;
    }
}
/**
 * 当前消费者是什么等级会员，如果是三级会员，只有三类级差奖，如果是二级会员，有二级级差和三级级差奖，如果是一级会员或者普通会员，有一级级差奖、二级级差奖、三级级差奖。
 */
if (!function_exists('jicha_level')){
    function jicha_level($money,$userid,$path_arr,$orderid){
        $first_status = $second_status = $third_status = 1;
        //$first_status1 = $second_status1 = $third_status1 = 1;
        //一类级差奖比例
        $first_proportion = \app\common\model\Reward::where('id',3)->value('proportion');
        //二类级差奖比例
        $second_proportion = \app\common\model\Reward::where('id',4)->value('proportion');
        //三类级差奖比例
        $third_proportion = \app\common\model\Reward::where('id',5)->value('proportion');
        foreach($path_arr as &$value){
            $this_userinfo = \app\common\model\User::where('id',$value)->field('group_id')->find();
            if($this_userinfo && $this_userinfo['group_id'] == 2){
                //一类级差奖
                $reward_money = $money*$first_proportion/100;
                if($reward_money >0.01 && $first_status){
                    $data = [
                        'userid'=>$value,
                        'money'=>$reward_money,
                        'type'=>3,
                        'order_id'=>$orderid
                    ];
                    Db::startTrans();
                    try {
                        $res = \app\common\model\UserMoneyLog::create($data);
                        if($res){
                            $res1 = \app\common\model\User::where('id',$value)->setInc('money',$reward_money);
                        }
                        Db::commit();
                    } catch (Exception $e) {
                        return $e->getMessage();
                        Db::rollback();
                        return $e->getMessage();
                    }
                    if($res && $res1){
                        $first_status  =  0;
                    }
                }
            }elseif($this_userinfo && $this_userinfo['group_id'] == 3){
                //二类级差奖
                if($first_status == 0){
                    $reward_money = $money*($second_proportion-$first_proportion)/100;
                }else{
                    $reward_money = $money*$second_proportion/100;
                }
                if($reward_money >0.01 && $second_status){
                    $data = [
                        'userid'=>$value,
                        'money'=>$reward_money,
                        'type'=>3,
                        'order_id'=>$orderid
                    ];
                    Db::startTrans();
                    try {
                        $res = \app\common\model\UserMoneyLog::create($data);
                        if($res){
                            $res1 = \app\common\model\User::where('id',$value)->setInc('money',$reward_money);
                        }
                        Db::commit();
                    } catch (Exception $e) {
                        return $e->getMessage();
                        Db::rollback();
                        return $e->getMessage();
                    }
                    if($res && $res1){
                        $first_status = $second_status = 0;
                    }
                }
            }elseif($this_userinfo && $this_userinfo['group_id'] == 4){
                //三类级差奖
                if($first_status == 0 && $second_status == 0){
                    $reward_money = $money*($third_proportion-$second_proportion-$first_proportion)/100;
                }elseif($first_status == 1 && $second_status == 0){
                    $reward_money = $money*($third_proportion-$second_proportion)/100;
                }elseif($first_status == 0 && $second_status == 1){
                    $reward_money = $money*($third_proportion-$first_proportion)/100;
                }else{
                    $reward_money = $money*$second_proportion/100;
                }
                if($reward_money >0.01 && $third_status){
                    $data = [
                        'userid'=>$value,
                        'money'=>$reward_money,
                        'type'=>3,
                        'order_id'=>$orderid
                    ];
                    Db::startTrans();
                    try {
                        $res = \app\common\model\UserMoneyLog::create($data);
                        if($res){
                            $res1 = \app\common\model\User::where('id',$value)->setInc('money',$reward_money);
                        }
                        Db::commit();
                    } catch (Exception $e) {
                        return $e->getMessage();
                        Db::rollback();
                        return $e->getMessage();
                    }
                    if($res && $res1){
                        $first_status = $second_status = $third_status = 0;
                    }
                }
            }
        }
    }
}
/**
 * 提取富文本字符串的纯文本,并进行截取;
 * @param $string 需要进行截取的富文本字符串
 * @param $int 需要截取多少位
 */
if (!function_exists('StringToText')){


    function StringToText($string,$num){
        if($string){
            //把一些预定义的 HTML 实体转换为字符
            $html_string = htmlspecialchars_decode($string);
            //将空格替换成空
            $content = str_replace("", "", $html_string);
            $content = str_replace("&nbsp;", "", $content);
            //函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
            $contents = strip_tags($content);
            //返回字符串中的前$num字符串长度的字符
            return mb_strlen($contents,'utf-8') > $num ? mb_substr($contents, 0, $num, "utf-8").'....' : mb_substr($contents, 0, $num, "utf-8");
        }else{
            return $string;
        }
    }
}
//生成订单号
if (!function_exists('getOrderSn')) {
    function getOrderSn($type = '')
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $num = round(0,9);
        $orderSn = $type.$yCode[$num] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5);
        $count = Db::name('order')->where('order_sn',$orderSn)->count();
        if($count){
            getOrderSn($type);
        }
        return $orderSn;
    }
}
//计算会员是否达到升级要求，如果足够就升级
if(!function_exists('check_parent_level')){
    function check_parent_level($userid){
        $user_info = \app\common\model\User::where('id',$userid)->field('id,path')->find();
        if($user_info){
            if($user_info['path'] != ''){
                $path = $user_info['path'].','.$user_info['id'];
            }else{
                $path = $user_info['id'];
            }
            //查询当前path下所有会员信息
            $user_list = \app\common\model\User::where('id','in',$path)->field('id,group_id,direct_num')->select();
            foreach($user_list as &$value){
                switch($value['group_id']){
                    //普通会员升级一级会员
                    case 1:
                       //查询普通会员升级一级会员的规则
                        $level_up = \app\common\model\UpgradeStandard::where('group_id',2)->value('xiaofei_num');
                        //查询当前用户消费总订单数
                        $order_count = \app\common\model\Order::where('userid',$value['id'])->where('status','in',1,2,3,6)->count();
                        //如果符合升级条件
                        if($order_count>=$level_up){
                            Db::startTrans();
                            try {
                                $res = \app\common\model\User::where('id',$value['id'])->setInc('group_id',1);
                                Db::commit();
                            } catch (Exception $e) {
                                return false;
                                Db::rollback();
                                return false;
                            }
                        }
                    break;
                    //一级会员升级二级会员
                    case 2:
                        //查询一级会员升级二级会员的规则
                        $level_up = \app\common\model\UpgradeStandard::where('group_id',3)->find();
                        //当前会员直推人数
                        $direct_num = $value['direct_num'];
                        //当前会员团队总消费金额
                        $child_list = \app\common\model\User::where('find_in_set(:id,path)',['id'=>$value['id']])->field('id')->select();
                        $ids  = [];
                        foreach($child_list as &$v1){
                            $ids[] = $v1['id'];
                        }
                        array_push($ids,$value['id']);
                        $total_money =\app\common\model\Order::where('userid','in',$ids)->where('status','in',1,2,3,6)->sum('price');
                        //如果符合升级条件
                        if($direct_num >= $level_up['direct_num'] && $total_money >= $level_up['team_xiaofei_money']){
                            Db::startTrans();
                            try {
                                $res = \app\common\model\User::where('id',$value['id'])->setInc('group_id',1);
                                Db::commit();
                            } catch (Exception $e) {
                                return false;
                                Db::rollback();
                                return false;
                            }
                        }
                        break;
                    //二级会员升级三级会员
                    case 3:
                        //查询二级会员升级三级会员的规则
                        $level_up = \app\common\model\UpgradeStandard::where('group_id',3)->find();
                        //当前会员直推人数
                        $direct_num = $value['direct_num'];
                        //当前会员团队总消费金额
                        $child_list = \app\common\model\User::where('find_in_set(:id,path)',['id'=>$value['id']])->field('id')->select();
                        $ids  = [];
                        foreach($child_list as &$v1){
                            $ids[] = $v1['id'];
                        }
                        array_push($ids,$value['id']);
                        $total_money =\app\common\model\Order::where('userid','in',$ids)->where('status','in',1,2,3,6)->sum('price');
                        //当前会员下级是二级会员的人数
                        $child_two_group = \app\common\model\User::where('parent_id',$value['id'])->where('group_id',3)->count();
                        //如果符合升级条件
                        if($direct_num >= $level_up['direct_num'] && $total_money >= $level_up['team_xiaofei_money'] && $child_two_group >=$level_up['twogroup_num']){
                            Db::startTrans();
                            try {
                                $res = \app\common\model\User::where('id',$value['id'])->setInc('group_id',1);
                                Db::commit();
                            } catch (Exception $e) {
                                return false;
                                Db::rollback();
                                return false;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
/*
*  富文本图片增加域名
*  $content    文本内容
*  $suffix     域名
* */
if(!function_exists('get_img_thumb_url')){

    function get_img_thumb_url($content = "", $suffix = "")
    {
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
        $pregRule1 = "/<[video].*?src=[\'|\"](.*?(?:[\.mp4]))[\'|\"].*?[\/]?>/";
        $content1 = preg_replace($pregRule, '<img src="' . $suffix . '${1}" >', $content);
        $content = preg_replace($pregRule1, '<video src="' . $suffix . '${1}" ">', $content1);
        return $content;
    }
}
if (!function_exists('__')) {

    /**
     * 获取语言变量值
     * @param string $name 语言变量名
     * @param array  $vars 动态变量值
     * @param string $lang 语言
     * @return mixed
     */
    function __($name, $vars = [], $lang = '')
    {
        if (is_numeric($name) || !$name) {
            return $name;
        }
        if (!is_array($vars)) {
            $vars = func_get_args();
            array_shift($vars);
            $lang = '';
        }
        return \think\Lang::get($name, $vars, $lang);
    }
}

if (!function_exists('format_bytes')) {

    /**
     * 将字节转换为可读文本
     * @param int    $size      大小
     * @param string $delimiter 分隔符
     * @return string
     */
    function format_bytes($size, $delimiter = '')
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 6; $i++) {
            $size /= 1024;
        }
        return round($size, 2) . $delimiter . $units[$i];
    }
}

if (!function_exists('datetime')) {

    /**
     * 将时间戳转换为日期时间
     * @param int    $time   时间戳
     * @param string $format 日期时间格式
     * @return string
     */
    function datetime($time, $format = 'Y-m-d H:i:s')
    {
        $time = is_numeric($time) ? $time : strtotime($time);
        return date($format, $time);
    }
}

if (!function_exists('human_date')) {

    /**
     * 获取语义化时间
     * @param int $time  时间
     * @param int $local 本地时间
     * @return string
     */
    function human_date($time, $local = null)
    {
        return \fast\Date::human($time, $local);
    }
}

if (!function_exists('cdnurl')) {

    /**
     * 获取上传资源的CDN的地址
     * @param string  $url    资源相对地址
     * @param boolean $domain 是否显示域名 或者直接传入域名
     * @return string
     */
    function cdnurl($url, $domain = false)
    {
        $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
        $url = preg_match($regex, $url) ? $url : \think\Config::get('upload.cdnurl') . $url;
        if ($domain && !preg_match($regex, $url)) {
            $domain = is_bool($domain) ? request()->domain() : $domain;
            $url = $domain . $url;
        }
        return $url;
    }
}


if (!function_exists('is_really_writable')) {

    /**
     * 判断文件或文件夹是否可写
     * @param string $file 文件或目录
     * @return    bool
     */
    function is_really_writable($file)
    {
        if (DIRECTORY_SEPARATOR === '/') {
            return is_writable($file);
        }
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . md5(mt_rand());
            if (($fp = @fopen($file, 'ab')) === false) {
                return false;
            }
            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
            return true;
        } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
            return false;
        }
        fclose($fp);
        return true;
    }
}

if (!function_exists('rmdirs')) {

    /**
     * 删除文件夹
     * @param string $dirname  目录
     * @param bool   $withself 是否删除自身
     * @return boolean
     */
    function rmdirs($dirname, $withself = true)
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }
}

if (!function_exists('copydirs')) {

    /**
     * 复制文件夹
     * @param string $source 源文件夹
     * @param string $dest   目标文件夹
     */
    function copydirs($source, $dest)
    {
        if (!is_dir($dest)) {
            mkdir($dest, 0755, true);
        }
        foreach (
            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
                RecursiveIteratorIterator::SELF_FIRST
            ) as $item
        ) {
            if ($item->isDir()) {
                $sontDir = $dest . DS . $iterator->getSubPathName();
                if (!is_dir($sontDir)) {
                    mkdir($sontDir, 0755, true);
                }
            } else {
                copy($item, $dest . DS . $iterator->getSubPathName());
            }
        }
    }
}

if (!function_exists('mb_ucfirst')) {
    function mb_ucfirst($string)
    {
        return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
    }
}

if (!function_exists('addtion')) {

    /**
     * 附加关联字段数据
     * @param array $items  数据列表
     * @param mixed $fields 渲染的来源字段
     * @return array
     */
    function addtion($items, $fields)
    {
        if (!$items || !$fields) {
            return $items;
        }
        $fieldsArr = [];
        if (!is_array($fields)) {
            $arr = explode(',', $fields);
            foreach ($arr as $k => $v) {
                $fieldsArr[$v] = ['field' => $v];
            }
        } else {
            foreach ($fields as $k => $v) {
                if (is_array($v)) {
                    $v['field'] = isset($v['field']) ? $v['field'] : $k;
                } else {
                    $v = ['field' => $v];
                }
                $fieldsArr[$v['field']] = $v;
            }
        }
        foreach ($fieldsArr as $k => &$v) {
            $v = is_array($v) ? $v : ['field' => $v];
            $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
            $v['primary'] = isset($v['primary']) ? $v['primary'] : '';
            $v['column'] = isset($v['column']) ? $v['column'] : 'name';
            $v['model'] = isset($v['model']) ? $v['model'] : '';
            $v['table'] = isset($v['table']) ? $v['table'] : '';
            $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']);
        }
        unset($v);
        $ids = [];
        $fields = array_keys($fieldsArr);
        foreach ($items as $k => $v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
                }
            }
        }
        $result = [];
        foreach ($fieldsArr as $k => $v) {
            if ($v['model']) {
                $model = new $v['model'];
            } else {
                $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']);
            }
            $primary = $v['primary'] ? $v['primary'] : $model->getPk();
            $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}");
        }

        foreach ($items as $k => &$v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $curr = array_flip(explode(',', $v[$n]));

                    $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr));
                }
            }
        }
        return $items;
    }
}

if (!function_exists('var_export_short')) {

    /**
     * 返回打印数组结构
     * @param string $var 数组
     * @return string
     */
    function var_export_short($var)
    {
        return VarExporter::export($var);
    }
}

if (!function_exists('letter_avatar')) {
    /**
     * 首字母头像
     * @param $text
     * @return string
     */
    function letter_avatar($text)
    {
        $total = unpack('L', hash('adler32', $text, true))[1];
        $hue = $total % 360;
        list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);

        $bg = "rgb({$r},{$g},{$b})";
        $color = "#ffffff";
        $first = mb_strtoupper(mb_substr($text, 0, 1));
        $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" alignment-baseline="central">' . $first . '</text></svg>');
        $value = 'data:image/svg+xml;base64,' . $src;
        return $value;
    }
}

if (!function_exists('hsv2rgb')) {
    function hsv2rgb($h, $s, $v)
    {
        $r = $g = $b = 0;

        $i = floor($h * 6);
        $f = $h * 6 - $i;
        $p = $v * (1 - $s);
        $q = $v * (1 - $f * $s);
        $t = $v * (1 - (1 - $f) * $s);

        switch ($i % 6) {
            case 0:
                $r = $v;
                $g = $t;
                $b = $p;
                break;
            case 1:
                $r = $q;
                $g = $v;
                $b = $p;
                break;
            case 2:
                $r = $p;
                $g = $v;
                $b = $t;
                break;
            case 3:
                $r = $p;
                $g = $q;
                $b = $v;
                break;
            case 4:
                $r = $t;
                $g = $p;
                $b = $v;
                break;
            case 5:
                $r = $v;
                $g = $p;
                $b = $q;
                break;
        }

        return [
            floor($r * 255),
            floor($g * 255),
            floor($b * 255)
        ];
    }
}

if (!function_exists('check_nav_active')) {
    /**
     * 检测会员中心导航是否高亮
     */
    function check_nav_active($url, $classname = 'active')
    {
        $auth = \app\common\library\Auth::instance();
        $requestUrl = $auth->getRequestUri();
        $url = ltrim($url, '/');
        return $requestUrl === str_replace(".", "/", $url) ? $classname : '';
    }
}

if (!function_exists('check_cors_request')) {
    /**
     * 跨域检测
     */
    function check_cors_request()
    {
        if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) {
            $info = parse_url($_SERVER['HTTP_ORIGIN']);
            $domainArr = explode(',', config('fastadmin.cors_request_domain'));
            $domainArr[] = request()->host(true);
            if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
                header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
            } else {
                header('HTTP/1.1 403 Forbidden');
                exit;
            }

            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');

            if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
                if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
                    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
                }
                if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
                    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
                }
                exit;
            }
        }
    }
}

if (!function_exists('xss_clean')) {
    /**
     * 清理XSS
     */
    function xss_clean($content, $is_image = false)
    {
        return \app\common\library\Security::instance()->xss_clean($content, $is_image);
    }
}
